/*
 * @Author: lee
 * @Date: 2023-10-25 15:50:05
 * @LastEditors: lee
 * @LastEditTime: 2023-10-25 19:19:43
 * @Description: file content
 */
const koa = require('koa');
const path = require('path');
const koaStatic = require('koa-static')
const koaSession = require('koa-session')
const cors = require('koa2-cors');


const routerResponse = require('./middleware/routerResponse')

const config = require('./config/index')

const router = require('./routes')
// const response = require('./middleware/response')
// 创建服务器
const app = new koa();

// 链接数据库
require('./utils/connect')

//使用session
const session = koaSession({
    renew: true
}, app)
app.keys = [config.KEYS]

app.use(session)



// 获取post数据
app.use(require('koa-body')({ multipart: true }));

// 静态服务器
app.use(koaStatic(path.resolve(__dirname, 'public')));

//中间件必须放在路由之前，否则报错

app.use(cors({
    origin: "*", // 允许来自指定域名请求
    maxAge: 5, // 本次预检请求的有效期，单位为秒。
    methods: ['GET', 'POST'], // 所允许的HTTP请求方法
    alloweHeaders: ['Conten-Type'], // 服务器支持的所有头信息字段
    credentials: true // 是否允许发送Cookie
}))

app.use(routerResponse())


app.use(router.routes(), router.allowedMethods());


app.listen(config.SERVER_PORT, () => console.log(`启动了服务器：${config.SERVER_URL}`))